import scrapy

from scrapys.wudiSpyder.wudiSpyder.items import JingcaiSpfGameItem


class Wan500JingcaiSpfSpider(scrapy.Spider):
    name = "Wan500JingcaiSpfSpider"
    allowed_domains = ["500.com"]
    start_urls = [
        "https://trade.500.com/jczq",  # 500wan 足球胜平负，让球胜平负竞猜彩选购页面
    ]

    def parse(self, response):
        print("正在房屋")
        tables = response.xpath('//*[@id="relativeContainer"]/div/div[3]/table')
        print(tables)
        for table in tables:
            trs = table.xpath("tbody/tr")
            for tr in trs:
                fid = tr.xpath("@data-fixtureid").get("data")  # 500wan比赛id
                expect = tr.xpath("@data-processdate").get("data")  # 竞猜比赛期数
                stop_time = tr.xpath("@data-buyendtime").get("data")  # 停止购买时间
                game_no_num = tr.xpath("@data-processname").get("data")  # 竞猜比赛场次
                game_no = tr.xpath("@data-matchnum").get("data")  # 竞猜比赛场次名称
                rang_qiu = tr.xpath("td[5]/p[2]").get("data").strip()  # 让球
                danguans = tr.xpath("@data-subactive").get("data").strip().split(",")  # 查找单关
                spf_single = 0  # 默认没有开
                rspf_single = 0
                for d in danguans:
                    if d == 'nspfdg:1':   # 胜平负 开单关
                        spf_single = 1
                    if d == 'spfdg:1':   # 让球胜平负 开单关
                        rspf_single = 1

                # f防止未开售的比赛
                spf_s = 0
                spf_p = 0
                spf_f = 0
                if len(tr.xpath("td[6]/div[1]/div").extract()) == 0:
                    spf_s = tr.xpath("td[6]/div[1]/p[@data-value='3']/@data-sp").get("data")    # 不让球胜的赔率
                    spf_p = tr.xpath("td[6]/div[1]/p[@data-value='1']/@data-sp").get("data")  # 不让球胜的赔率
                    spf_f = tr.xpath("td[6]/div[1]/p[@data-value='0']/@data-sp").get("data")  # 不让球胜的赔率

                spf_bonus = "{},{},{}".format(spf_s, spf_p, spf_f)

                rspf_s = tr.xpath("td[6]/div[2]/p[@data-value='3']/@data-sp").get("data")    # 不让球胜的赔率
                rspf_p = tr.xpath("td[6]/div[2]/p[@data-value='1']/@data-sp").get("data")  # 不让球胜的赔率
                rspf_f = tr.xpath("td[6]/div[2]/p[@data-value='0']/@data-sp").get("data")  # 不让球胜的赔率
                rspf_bonus = "{},{},{}".format(rspf_s, rspf_p, rspf_f)
                # 提取游戏信息
                game_item = JingcaiSpfGameItem()
                game_item['fid'] = fid
                game_item['expect'] = expect
                game_item['stop_time'] = stop_time
                game_item['game_no'] = game_no
                game_item['game_no_num'] = game_no_num
                game_item['rang_qiu'] = rang_qiu
                game_item['spf_single'] = spf_single
                game_item['rspf_single'] = rspf_single
                game_item['spf_bonus'] = spf_bonus
                game_item['rspf_bonus'] = rspf_bonus
                print(game_item)
                yield game_item



